package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 前端目的
 */

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    //首页
    @RequestMapping("")
    public String index(Model model){
        //hotRegions
        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions", regions);
        return "destination/index";
    }



    //热门的数据
    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId){
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        int size = list.size();
        //leftDests
        List<Destination> leftDests = list.subList(0, size/2 + 1);
        //rightDests
        List<Destination> rightDests = list.subList(size/2 + 1,size);

/*

        List<Destination> list1 = new ArrayList<>();
        List<Destination> list2 = new ArrayList<>();
        int min = size /2;
        if(list != null && size > 0){
            for(int i = 0; i< size; i++){
                if(i < min){
                    list1.add(list.get(i));
                }else {
                    list2.add(list.get(i));
                }
            }
        }
*/

        model.addAttribute("regionId", regionId);
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        return "destination/hotdestTpl";
    }


    @RequestMapping("/guide")
    public String guide(Model model, Long id){

        List<Destination> toasts = destinationService.getToasts(id);

        Destination dest = toasts.remove(toasts.size() - 1);
        //toasts
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", dest);


        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogsByDestId(id);
        model.addAttribute("catalogs", catalogs);

        //strategyDetails: 点击量前3的攻略明细

        List<StrategyDetail> list = strategyDetailService.queryStratgyTop3(id);
        model.addAttribute("strategyDetails", list);

        return  "destination/guide";

    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){

        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        Destination dest = toasts.remove(toasts.size() - 1);
        //toasts
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", dest);

        return  "destination/survey";

    }
    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){

        //catalogs : 攻略分类类表
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogsByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);

        StrategyCatalog catalog = null;

        if(catalogs != null && catalogs.size() > 0){
            for (StrategyCatalog c : catalogs) {
                if(c.getId() == qo.getCatalogId()){
                    catalog = c;
                    //catalog ： 当前选中分类对象
                    model.addAttribute("catalog", catalog);
                    break;
                }
            }
        }

        if(catalog != null){
            List<StrategyDetail> details = catalog.getDetails();
            if(details != null && details.size() > 0){
                StrategyDetail detail = details.get(0);
                StrategyContent content = strategyDetailService.getContent(detail.getId());
                detail.setStrategyContent(content);
                //detail ： 当前选中分类对象关联的攻略明细列表的第一条
                model.addAttribute("detail", detail);
            }
        }
        return  "destination/surveyTpl";

    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo")TravelQuery qo){

        PageInfo page = travelService.query(qo);

        model.addAttribute("pageInfo", page);
        return "destination/travelTpl";
    }


}
